package com.system.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.staff.entity.Staff;
import com.system.dto.PermissionDTO;

@Entity
@Table(name="t_role")
public class Role 
{
	private Long id;
	private String name;
	private String description;
	private List<Permission> PermissionList=new ArrayList<Permission>();
	private List<Staff> staffList=new ArrayList<Staff>();
	
	
	//业务字段
	private Boolean isSelect=false;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}
	@Column(unique=true,nullable=false)
	public String getName() {
		return name;
	}
	public String getDescription() {
		return description;
	}
	@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	@JoinTable(name="t_role_permission",joinColumns=@JoinColumn(name="role_id"),inverseJoinColumns=@JoinColumn(name="permission_id"))
	@JsonIgnore
	public List<Permission> getPermissionList() {
		return PermissionList;
	}
	@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	@JoinTable(name="t_role_user",joinColumns=@JoinColumn(name="role_id"),inverseJoinColumns=@JoinColumn(name="user_id"))
	@JsonIgnore
	public List<Staff> getStaffList() {
		return staffList;
	}
	@Transient
	public Boolean getIsSelect() {
		return isSelect;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public void setPermissionList(List<Permission> permissionList) {
		this.PermissionList = permissionList;
	}
	public void setStaffList(List<Staff> staffList) {
		this.staffList = staffList;
	}
	
	
	public void setIsSelect(Boolean isSelect) {
		this.isSelect = isSelect;
	}
	public static Specification<Role> getSpecification( final Role role)
	{
		Specification<Role> spec = new Specification<Role>() {
			public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != role && !StringUtils.isEmpty(role.getId())) {
					 Predicate  p1 =  cb.equal(root.get("id").as(String.class),role.getId());
					 list.add(p1);
				 }
				 if(null != role && !StringUtils.isEmpty(role.getName())) {
					 Predicate  p2 =  cb.like(root.get("description").as(String.class),"%"+ role.getName()+ "%");
					 list.add(p2);
				 }
				 if(null != role && !StringUtils.isEmpty(role.getDescription())) {
					 Predicate  p3 =  cb.like(root.get("description").as(String.class),"%"+ role.getDescription()+ "%");
					 list.add(p3);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;	
	}
}
